<!--
 * @页面名称:触发器
 * @描述: 
 * @作者: 
 * @Date:
-->
<template>
  <div class="form-tab4">
    <!-- 按钮组 -->
    <div class="button-group">
      <el-tooltip class="box-item" placement="bottom-end">
        <template #content>
          <span>添加界面</span>
        </template>
        <el-button :disabled="BtnStatus[0]" :icon="FolderAdd" @click="add"></el-button>
      </el-tooltip>
      <el-tooltip class="box-item" placement="bottom-end">
        <template #content>
          <span>修改界面</span>
        </template>
        <el-button :disabled="BtnStatus[1]" :icon="Edit" @click="edit"></el-button>
      </el-tooltip>
      <el-tooltip class="box-item" placement="bottom-end">
        <template #content>
          <span>删除界面</span>
        </template>
        <el-button :disabled="BtnStatus[2]" :icon="Delete" @click="del"></el-button>
      </el-tooltip>
    </div>
    <!-- 表格模块 -->
    <stable :columns="tableSetting.columns" :data="tableSetting.data" @rowClick="rowClick"></stable>
    <!-- 新增修改弹出框 -->
    <el-dialog v-model="dialogSetting.visible" :title="dialogSetting.title" width="30%" draggable>
      <el-form label-width="90px" :model="formData" ref="formRef">
        <el-form-item label="触发器类型:" prop="Type">
          <el-select v-model="formData.Type">
            <el-option label="insert" value="insert" />
            <el-option label="delete" value="delete" />
            <el-option label="update" value="update" />
          </el-select>
        </el-form-item>
        <el-form-item label="顺序号:" prop="order">
          <el-input type="number" v-model="formData.order"></el-input>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="启用:" prop="Enable">
              <el-checkbox v-model="formData.Enable"></el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label-width="120px" label="子级联动触发:" prop="TR_IsTriggerByChild">
              <el-checkbox v-model="formData.TR_IsTriggerByChild"></el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="T_SQL:" prop="sql">
          <el-input v-model="formData.sql" :autosize="{ minRows: 2, maxRows: 4 }" type="textarea"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogSetting.visible = false">取消</el-button>
          <el-button type="primary" @click="handle"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="tsx">
import { ref, reactive, nextTick } from "vue";
import { FolderAdd, Edit, Delete } from "@element-plus/icons-vue";
import useCurrentInstance from "@/utils/useCurrentInstance";
import type { FormInstance, FormRules } from "element-plus";
// 按钮状态
const BtnStatus = ref([true, true, true]);
const { proxy } = useCurrentInstance();
// 父级表格保存对象
const tabClickFromParent = ref();
// 表格配置
const tableSetting = reactive({
  columns: [
    { title: "顺序号", width: 120, field: "TR_Order", sortable: true },
    { title: "类型", width: 120, field: "TR_Type", sortable: true },
    {
      title: "是否启用",
      field: "TR_Enable",
      width: 120,
      sortable: true,
      slots: {
        default: ({ row }: any) => {
          return [<el-checkbox disabled v-model={row.TR_Enable} />];
        }
      }
    },
    { title: "T_SQL", field: "TR_SQL", sortable: true }
  ],
  data: [] as any[]
});
// 表格单击保存对象
const clickObj = ref();
// 弹出框配置
const dialogSetting = reactive({
  visible: false,
  title: ""
});
// 表单绑定字段
const formData = reactive({
  Enable: true,
  sql: "",
  order: 1,
  Type: "",
  TR_IsTriggerByChild: true
});
// 表单实例
const formRef = ref<FormInstance>();
// 获取表格数据
const getTableData = (record: any) => {
  if (!record) {
    return;
  }
  tabClickFromParent.value = record;
  proxy.$vueAxios
    .post("Master_Base_TriggerRule/GetConditional", {
      order: "TR_Order asc",
      dtoList: [{ fieldName: "FMT_ID", fieldValue: record.FMT_ID }]
    })
    .then((res: any) => {
      tableSetting.data = res.List;
      tableSetting.data.sort((a: any, b: any) => a.TR_Order - b.TR_Order);
      BtnStatus.value[0] = false;
    });
};
// 表格单击事件
const rowClick = ({ row }: any) => {
  BtnStatus.value[1] = false;
  BtnStatus.value[2] = false;
  clickObj.value = row;
};
// 新增事件
const add = () => {
  formRef.value?.resetFields();
  dialogSetting.visible = true;
  dialogSetting.title = "新增";
};
// 修改事件
const edit = () => {
  dialogSetting.visible = true;
  dialogSetting.title = "修改";
  nextTick(() => {
    formData.order = clickObj.value.TR_Order;
    formData.Type = clickObj.value.TR_Type;
    formData.Enable = clickObj.value.TR_Enable;
    formData.sql = clickObj.value.TR_SQL;
  });
};
// 删除事件
const del = () => {
  proxy.$messageBox
    .confirm("确定删除该数据？", "提示", {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning"
    })
    .then(() => {
      proxy.$vueAxios.post("Master_Base_TriggerRule/Delete", [clickObj.value.TR_ID]).then((res: any) => {
        if (res.Mark) {
          tableSetting.data = tableSetting.data.filter(
            (v: any) => v !== tableSetting.data.find((v: any) => v.TR_ID === clickObj.value.TR_ID)
          );
          proxy.$message.success("删除成功");
        } else {
          proxy.$message.error("删除失败");
        }
      });
    })
    .catch(err => {
      console.log(err);
      proxy.$message("取消删除");
    });
};
// 新增修改确认事件
const handle = () => {
  formRef.value!.validate((isValid: boolean) => {
    if (isValid) {
      if (dialogSetting.title === "新增") {
        const param = {
          tR_ID: proxy.$global.createID(),
          fmT_ID: tabClickFromParent.value.FMT_ID,
          tR_Type: formData.Type,
          tR_Enable: formData.Enable,
          tR_Order: formData.order,
          tR_SQL: formData.sql,
          tR_IsTriggerByChild: formData.TR_IsTriggerByChild
        };
        proxy.$vueAxios.post("Master_Base_TriggerRule", param).then(() => {
          dialogSetting.visible = false;
          getTableData(tabClickFromParent.value);
        });
      } else {
        const param = {
          tR_ID: clickObj.value.TR_ID,
          fmT_ID: clickObj.value.FMT_ID,
          tR_Type: formData.Type,
          tR_Enable: formData.Enable,
          tR_Order: formData.order,
          tR_SQL: formData.sql,
          tR_IsTriggerByChild: formData.TR_IsTriggerByChild
        };
        proxy.$vueAxios.put("Master_Base_TriggerRule", param).then(() => {
          dialogSetting.visible = false;
          getTableData(tabClickFromParent.value);
        });
      }
    }
  });
};

// 将getTableData函数暴露给父组件
defineExpose({ getTableData });
</script>

<style scoped lang="scss">
.form-tab4 {
  height: calc(100% - 40px);
  .button-group {
    margin: 0 0 10px;
  }
}
</style>
